﻿using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WidgetsDesign.API.Dtos;
using WidgetsDesign.API.Models;
using WidgetsDesign.API.Services;

namespace WidgetsDesign.API.Controllers
{
    [Route("api/[controller]/[Action]")]
    [ApiController]
    public class TrainController : ControllerBase
    {
        private readonly ITrainRepository _trainRepository;
        private readonly IMapper _Mapper;
        public TrainController(ITrainRepository trainRepository, IMapper mapper)
        {
            _trainRepository = trainRepository;
            _Mapper = mapper;
        }
        [HttpGet]
        public async Task<IActionResult> UnderwayTrain()
        {
            var underwayTrain = await _trainRepository.UnderwayTrain();
            return Ok(new { ret = true, code = 200, data = underwayTrain, message = "创建成功！" });
        }
        [HttpPost]
        public async Task<IActionResult> CreateTrain([FromBody] TrainDto trainDto)
        {
            if (String.IsNullOrEmpty(trainDto.TrainId))
            {
                trainDto.TrainId = Guid.NewGuid().ToString();
                trainDto.CreateTime = DateTime.Now;
                trainDto.State = 1;
                var Model = _Mapper.Map<TrainModel>(trainDto);
                _trainRepository.CreateTrain(Model);
                await _trainRepository.SaveAsync();
                return Ok(new { ret = true, code = 200, data = trainDto.TrainId, message = "创建成功！" });
            }
            else
            {
                var Model = _Mapper.Map<TrainModel>(trainDto);
                _trainRepository.UpdateName(Model);
                await _trainRepository.SaveAsync();
                return Ok(new { ret = true, code = 200, data = trainDto.TrainId, message = "更新成功！" });
            }

        }
        [HttpPost]
        public async Task<IActionResult> UpdateEmployees([FromBody] TrainDto trainDto)
        {
            var Model = _Mapper.Map<TrainModel>(trainDto);
            _trainRepository.UpdateEmployees(Model);
            await _trainRepository.SaveAsync();
            return Ok(new { ret = true, code = 200, data = trainDto.TrainId, message = "创建成功！" });
        }
        [HttpGet]
        public async Task<IActionResult> DeleteErrorTrain()
        {
            _trainRepository.DeleteErrorTrain();
            await _trainRepository.SaveAsync();
            return Ok(new { ret = true, code = 200, message = "创建成功！" });
        }
        [HttpGet]
        public async Task<IActionResult> QueryTrainList(int pageIndex, int pageSize, DateTime? startTime, DateTime? endTime)
        {
            var result = await _trainRepository.queryList(pageIndex, pageSize, startTime, endTime);
            var total = await _trainRepository.getTotal(startTime, endTime);
            return Ok(new { ret = true, code = 200, data = result, total });
        }
        [HttpGet]
        public async Task<IActionResult> TrainById(string id)
        {
            var result = await _trainRepository.queryTrainById(id);
            return Ok(new { ret = true, code = 200, data = result });
        }
        [HttpGet]
        public async Task<IActionResult> DeleteTrainById(string id)
        {
            _trainRepository.DeleteTrainById(id);
            await _trainRepository.SaveAsync();
            return Ok(new { ret = true, code = 200, data = "ok" });
        }
        [HttpGet]
        public IActionResult SaveVideo(string id)
        {
            _trainRepository.SaveVideo(id);
            return Ok(new { ret = true, code = 200 });
        }
    }
}
